package com.ljfiction.book.tools;

import android.content.Context;
import android.util.Log;

import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.wechat.friends.Wechat;

import static com.ljfiction.book.tools.ToastUtils.showToast;


/**
 * Created by Android on 2018/3/5.
 */

public class ShareTool {
    public static void toShare(Context context, String bookName, String bookId, String url, String bookdata, String userId) {

        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("《" + bookName + "》");

        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(HttpManager.ShareIP + "/book/" + bookId);

        // text是分享文本，所有平台都需要这个字段
        oks.setText(bookdata);
        oks.setImageUrl(url);

        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        // oks.setImagePath(TEST_IMAGE);//确保SDcard下面存在此张图片

        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(HttpManager.ShareIP + "/book/" + bookId);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment(bookdata);
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Tool.showToast("分享成功", context);
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Tool.showToast("分享错误", context);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Tool.showToast("分享取消", context);
            }
        });
        // site是分享此内容的网站名称,仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));

        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(HttpManager.ShareIP + "/book/" + bookId);

        // 启动分享GUI
        oks.show(context);

    }
public static void toSharexiaochengxu(String bookId1,String bookName,String bookImage){
    Platform sina2 = ShareSDK.getPlatform(Wechat.NAME);
    Platform.ShareParams SP2 = new Platform.ShareParams();
    SP2.setWxUserName("gh_2f5fc9d63f16");  //小程序原始id
    SP2.setWxPath("pages/share?bookId=" + bookId1 + "&type="+2+"&userCode=LGYY001" + bookId1);//分享小程序页面路径
    //0-正式，1-开发，2-体验
    SP2.setWxMiniProgramType(0);
    SP2.setTitle("我的作品《"+bookName+"》已发布,快来添加收藏吧!");
    SP2.setText("https://www.lajixs.com");
    SP2.setImageUrl(bookImage);
    SP2.setUrl("https://www.lajixs.com");
    SP2.setWxWithShareTicket(true);
    SP2.setShareType(Platform.SHARE_WXMINIPROGRAM);//设置分享小程序类型
    sina2.setPlatformActionListener(new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            showToast("分享成功");
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            showToast("分享失败");
            Log.v("xiaochengxu",throwable.getMessage());
        }

        @Override
        public void onCancel(Platform platform, int i) {
            showToast("分享取消");
        }
    });
    sina2.share(SP2);
}

}
